//
//  Dictionary+SJAdd.swift
//  Employee
//
//  Created by Saxon on 2018/3/28.
//  Copyright © 2018年 remotevillage. All rights reserved.
//

import Foundation

public extension Dictionary {
  // MARK: - Functions
  
  public func safeObject(forKey key: Key) -> Any? {
    if let value = self[key] {
      return value
    } else {
      return nil
    }
  }
    public func signParame(_ secretkey:String) ->[String : Any]
    {
        var result:[String : Any] = [:]
        
        let array = self.keys.sorted { (k1, k2) -> Bool in
            return String(describing:k1) < String(describing:k2) ? true : false
//            return String(describing:k1).lowercased() < String(describing:k2).lowercased() ? true : false     //不区分大小写
        }
        var datas:[String] = []
        
        for key in array {
            let value = self[key]!
            result[String(describing:key)] = value
            if ("\(key)" == "access_token") {
                continue
            }
            let str = "\(key)=\(value)"
            datas.append(str)
        }
        if (datas.count > 0) {
            datas.append("signkey=\(secretkey)")
            
            let signKey = datas.joined(separator: "&")
            if SJToolkitConfig.DEBUG {
                print("sign====\(signKey)")
            }
            
            result["sign"] = signKey.md5()
        }
        return result
    }
}
